// ==UserScript==
// @name         Conversion to Objects
// @namespace    http://tampermonkey.net/
// @version      2024-05-17
// @description  try to take over the world!
// @author       You
// @match        https://thepiquelab.atlassian.net/wiki/spaces/UIUX/pages/803963259/A+Manage+Permissions
// @icon         https://www.google.com/s2/favicons?sz=64&domain=atlassian.net
// @grant        GM_addElement
// @grant        GM_setClipboard
// @grant        GM_notification
// ==/UserScript==

(function() {
    'use strict';

    const bt = GM_addElement(
        document.body,
        'button',
        {
            textContent: 'Conversion to Objects',
            style: "position: fixed; bottom: 10px; right: 10px;"
        }
    );
    bt.addEventListener("click", function() {
        const obj = convert();

        bt.textContent = `permission:${Object.keys(obj).length}, effect:${Object.values(obj).reduce((memo, item) => memo + item.length, 0)}`

        GM_setClipboard(JSON.stringify(obj), "text", () => {
            // alert("Clipboard set!");
            GM_notification({ text: `Copied.`});
        });
    }, false);

    function convert(){
        const result = {};
        const tbodies = document.querySelectorAll("table tbody");
        tbodies.forEach((tbody) => {
            const rows = tbody.querySelectorAll("tr");
            rows.forEach((row) => {
                const tdCount = row.querySelectorAll("td").length;
                let codePositioin = 3;
                let effectPosition = 5;
                if (tdCount === 6) {
                    codePositioin = 4;
                    effectPosition = 6;
                }
                const thirdColumnCell = row.querySelector(`td:nth-child(${codePositioin}) code`);
                if (thirdColumnCell) {
                    const permision = thirdColumnCell.textContent.trim();
                    const pElement = row.querySelectorAll(`td:nth-child(${effectPosition}) p`);
                    if (pElement.length > 0) {
                        pElement.forEach((p) => {
                            const p_ = p.cloneNode(true);
                            const codeElements = p_?.querySelectorAll("code");
                            codeElements?.forEach((codeElement) => {
                                const textContent = codeElement.textContent;
                                const newTextNode = document.createTextNode(`\`${textContent}\``);
                                codeElement.replaceWith(newTextNode);
                            });
                            const behavior = p_.textContent.trim();
                            result[permision] = result[permision]
                                ? [...result[permision], behavior]
                            : [behavior];

                            const academicIndex = result[permision].findIndex(
                                (i) => i === "Academic"
                            );
                            if (academicIndex > 0) {
                                result[permision] = result[permision].slice(0, academicIndex);
                            }
                            result[permision] = result[permision].filter(i => !['', "CRM"].includes(i))
                        });
                    }
                }
            });
        });
        return result;
    }
})();